package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.RankingBoardIncomeConf;
import com.sc.nft.entity.vo.AdminRankingBoardIncomeDetailsVO;
import com.sc.nft.mapper.RankingBoardIncomeConfMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 排行榜收益配置表(sc_ranking_board_income_conf)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-08-03 16:44:33
 */
@Slf4j
@Repository
public class RankingBoardIncomeConfDao extends ServiceImpl<RankingBoardIncomeConfMapper, RankingBoardIncomeConf> {

    public RankingBoardIncomeConf getLastOneByRankingBoardConfId(Long rankingBoardConfId) {
        LambdaQueryWrapper<RankingBoardIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardIncomeConf::getRankingBoardConfId, rankingBoardConfId);
        wrapper.orderByDesc(RankingBoardIncomeConf::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public List<AdminRankingBoardIncomeDetailsVO> getRankingBoardIncomeDetails(Long rankingBoardConfId) {
        return baseMapper.getRankingBoardIncomeDetails(rankingBoardConfId);
    }

    public Integer getCountByRankingBoardConfIdAndBeginSectionAndEndSection(Long id, Integer beginSection, Integer endSection) {
        LambdaQueryWrapper<RankingBoardIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardIncomeConf::getRankingBoardConfId, id);
        wrapper.eq(RankingBoardIncomeConf::getBeginSection, beginSection);
        wrapper.eq(RankingBoardIncomeConf::getEndSection, endSection);
        return count(wrapper);
    }

    public boolean delRankingBoardIncomeDetails(Long rankingBoardConfId, Integer beginSection, Integer endSection) {
        LambdaQueryWrapper<RankingBoardIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardIncomeConf::getRankingBoardConfId, rankingBoardConfId);
        wrapper.eq(RankingBoardIncomeConf::getBeginSection, beginSection);
        wrapper.eq(RankingBoardIncomeConf::getEndSection, endSection);
        return remove(wrapper);
    }

    /**
     * 根据排名和排行榜配置id获取排行榜收益配置
     *
     * @param rankingBoardConfId
     * @param ranking
     * @return
     */
    public RankingBoardIncomeConf getByRankingAndRankingBoardConfId(Long rankingBoardConfId, Integer ranking) {
        LambdaQueryWrapper<RankingBoardIncomeConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RankingBoardIncomeConf::getRankingBoardConfId, rankingBoardConfId);
        wrapper.eq(RankingBoardIncomeConf::getRanking, ranking);
        wrapper.orderByDesc(RankingBoardIncomeConf::getId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public RankingBoardIncomeConf getByRankingBoardConfId(Long id, Integer ranking) {
        LambdaQueryWrapper<RankingBoardIncomeConf> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RankingBoardIncomeConf::getRankingBoardConfId, id);
        // 等级大于等于最小 小于等于最大
        wrapper.le(RankingBoardIncomeConf::getBeginSection, ranking);
        wrapper.ge(RankingBoardIncomeConf::getEndSection, ranking);
        wrapper.orderByAsc(RankingBoardIncomeConf::getRanking);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

}